package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author Zwnan
 */
@Service
public class FilterChainManagerImpl implements FilterChainManager {

    @Autowired
    private Map<String, FilterChainService> filterMap;

    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private PushMsgUtil pushMsgUtil;

    @Override
    public void executeFilter(StandardSubmit submit) {



        String filterOrder = cacheFeign.hget(CacheConstants.CACHE_PREFIX_CLIENT+submit.getClientID(), "filters");

        if (StringUtils.isEmpty(filterOrder)){
            return;
        }

        String[] filters = filterOrder.split(",");
        for (String filter : filters) {
            FilterChainService filterChainService = filterMap.get(filter);
            if (filterChainService ==null){
                continue;
            }
            boolean flag = filterChainService.stratrgy(submit);
            if (!flag){
                pushMsgUtil.sendLog(submit,filter);
                pushMsgUtil.pushReport(submit,filter);
                return;
            }

        }



    }
}
